package com.jack.configuration;

import java.io.IOException;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.atomikos.jdbc.AtomikosDataSourceBean;

@Configuration
@EnableConfigurationProperties
@EnableAutoConfiguration
@MapperScan(basePackages = {"com.jack.mapper.income"}, sqlSessionTemplateRef = "jtaIncomeSqlSessionTemplate")
public class DataSourceJTAIncomeConfig {

	@Bean
	@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.jta-income")
	public DataSource  jtaIncomeDataSource() {
		return new AtomikosDataSourceBean();
	}
	
	
	@Bean
	public SqlSessionFactory jtaIncomesqlSesionFactory(@Qualifier("jtaIncomeDataSource")DataSource jtaIncomeDataSource) throws Exception {
		 SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		 bean.setDataSource(jtaIncomeDataSource);
		 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
		 bean.setTypeAliasesPackage("com.jack.mapper.income");
		 return bean.getObject();
	}
	
	@Bean 
	public SqlSessionTemplate jtaIncomeSqlSessionTemplate(@Qualifier("jtaIncomesqlSesionFactory")SqlSessionFactory jtaIncomesqlSesionFactory) {
		return new SqlSessionTemplate(jtaIncomesqlSesionFactory);
		
	}
	
}
